<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@page import="org.json.JSONObject"%>
<%@page import="db.SubstoreDAO"%>
<%@page import="db.Substore"%>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<% 
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	ReceivepurmatDAO receivepurmatDao = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	SubstoreDAO substoreDao = (SubstoreDAO)ctx.getBean("SubstoreDAO");
	
	String substore = request.getParameter("substore");
	String type = request.getParameter("type");
	String keyword = request.getParameter("keyword");
	String ids = request.getParameter("ids");
	String open = request.getParameter("open");
	String proCode = request.getParameter("proCode");
	
	List substoreList = substoreDao.findAll();
	request.setAttribute("substoreList",substoreList);
	
	String[] idArray = null;
	if(ids != null){
		idArray = ids.split(",");
	}
		
	Integer tid = (Integer)session.getAttribute("tid");
	
	List where = new ArrayList();
	
	where.add(new Object[]{"eq", "isDelete", 0});
	where.add(new Object[]{"eq", "teamId", tid});
	where.add(new Object[]{"ge", "useNum", 1});
	if(substore != null && !substore.trim().equals("")){
		where.add(new Object[]{"eq", "subStore", Integer.parseInt(substore)});
		Substore substoreObj = substoreDao.findById(Integer.parseInt(substore));
		if(substoreObj.getName().equals("专项") && (proCode != null && !"".equals(proCode.trim()))){
			where.add(new Object[]{"eq", "proCode", proCode});
		}
	}
	
	if("1".equals(type)){
		where.add(new Object[]{"like", "m.code", keyword});
	}else if("2".equals(type)){
		where.add(new Object[]{"like", "m.name", keyword});
	}else if("3".equals(type)){
		where.add(new Object[]{"eq", "batch", keyword});
	}
	
	if(idArray != null)
	for(int i=0;i<idArray.length;i++){
		if(!"".equals(idArray[i].trim())){
			where.add(new Object[]{"ne", "id", Integer.parseInt(idArray[i])});
		}
	}
	
	if(open == null){
		Map map = receivepurmatDao.getReceivepurmat(0, 0,
				new String[][]{new String[]{"desc","date"}},
				where,
	     		new String[][]{new String[]{"","id"},new String[]{"","m.code"},new String[]{"","m.name"},new String[]{"","batch"},new String[]{"","m.company"},new String[]{"","useNum"},new String[]{"","p.name"},new String[]{"","purPrice"}}		
				,new String[]{"material","place"});
		
		request.setAttribute("map",map);
	
		JSONObject json = new JSONObject();
		json.put("data",map.get("data"));
		request.setAttribute("jsonData",json.toString());
	}
	
	request.setAttribute("proCode",proCode);
	request.setAttribute("ids",ids);
	request.setAttribute("substore",substore);
	request.setAttribute("type",type);
	request.setAttribute("keyword",keyword);

	pageContext.forward("/vm/outStock/openWin.vm");
%>